//获取某天的起止时间戳（今日传入0，昨日传入1，明日传入-1）
import { useRef } from 'react'
import { useSize } from 'ahooks'
// 是否mac平台
export const isMac = (/macos/i.test(typeof window != 'undefined' && window?.navigator.userAgentData?.platform)) || (/macintosh|mac os x/i.test(typeof window != 'undefined' && navigator.userAgent));
// 是否safari
export const isSafari = isMac && /safari/i.test(typeof window != 'undefined' && window?.navigator.userAgent) && !/chrome/i.test(typeof window != 'undefined' && navigator.userAgent);

export function getStartEndTime (num = 0) {
  // 一天的毫秒数
  const MillisecondsADay = 24 * 60 * 60 * 1000 * num
  // 今日开始时间戳
  const todayStartTime = new Date(new Date().toLocaleDateString()).getTime()
  // 昨日开始时间戳
  const tomorrowEndTime = new Date(new Date()).getTime() + (24 * 60 * 60 * 1000)
  // 昨日结束时间戳
  return {
    tomorrowEndTime,
    todayStartTime,
  }
}


export const week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']

/**
 * 兼容处理 播放地址转为数组
 */
export const liveUrlTransformToArray = (playUrl) => {

  if (Array.isArray(playUrl)) {
    return playUrl.map(({ liveUrl, showName: name }) => {
      const url = isSafari ? liveUrl.replace(/\.flv$|\.flv(\?)/i, '.m3u8$1') : liveUrl;
      const protocolType = /m3u8/i.test(url) ? 'm3u8' : 'flv';
      return {
        name,
        playUrl: url,
        protocolType,
      };
    });
  }
  if (typeof playUrl === 'string') {
    const url = isSafari ? playUrl.replace(/\.flv$|\.flv(\?)/i, '.m3u8$1') : playUrl;
    const protocolType = /m3u8/i.test(url) ? 'm3u8' : 'flv';
    // 替换高清url 后台已经改了（留着兼容一下）
    const urlReplace = playUrl.replace(/\.flv$|\.flv(\?)/i, '_lud.flv$1');
    const ludUlr = isSafari ? urlReplace.replace(/\.flv$|\.flv(\?)/i, '.m3u8$1') : urlReplace;
    return [{
      name: '原画',
      playUrl: url,
      protocolType,
    }];
  }
  return playUrl;
};

export const isMobile = (navigator) => {
  return /(iPhone|iPad|iPod|iOS|Android|Linux armv8l|Linux armv7l|Linux aarch64)/i.test(navigator);
}


export const remToPx = (px) => {
  if (typeof window === 'undefined') return 0
  const width = document.documentElement.clientWidth
  return width / 750 * px
}
// 获取当前视频链接




export const useLiveListHeight = (otherSize = 0) => {
  const leftRef = useRef()
  const rightRef = useRef()
  const leftSize = useSize(leftRef)
  const rightSize = useSize(rightRef)
  const liveHeight = rightSize?.height - leftSize?.height - otherSize + "px"
  return { leftRef, rightRef, liveHeight }
}
